package yangan.utils;

import app.nbdevices.utils.ConvertUtil;
import yangan.entity.YanganDataReport;
import yangan.modle.codecModel.YanganCycleReport;
import yangan.modle.codecModel.YanganEventReport;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class YanganExplain {
    /**
     * 解释事件上报
     * @param yanganEventReport
     * @return
     */
    public static YanganDataReport explainYanganEvent(YanganEventReport yanganEventReport){
        YanganDataReport yanganDataReport=new YanganDataReport();
        yanganDataReport.setAddrId(ConvertUtil.getBase64Codec().
                encodeToString(yanganEventReport.getNBNumber()));
        yanganDataReport.setCId(explainCID(yanganEventReport.getStateCode()));
        yanganDataReport.setYanganSignal(Integer.parseInt
                (ConvertUtil.byteToString(yanganEventReport.getSignal())));
        yanganDataReport.setYanganVoltage((float)Byte.toUnsignedInt(yanganEventReport.getVoltage())/10);
        yanganDataReport.setNbProtect(Byte.toUnsignedInt(yanganEventReport.getProtectNum()));
        yanganDataReport.setPageNum(Byte.toUnsignedInt(yanganEventReport.getPageNum()));
        yanganDataReport.setReportTime(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        return yanganDataReport;
    }

    /**
     * 解释周期上报
     * @param yanganCycleReport
     * @return
     */
    public static YanganDataReport explainYanganCycle(YanganCycleReport yanganCycleReport){
        YanganDataReport yanganDataReport=new YanganDataReport();
        yanganDataReport.setAddrId(ConvertUtil.getBase64Codec().
                          encodeToString(yanganCycleReport.getNBNumber()));
        yanganDataReport.setCId(explainCID(yanganCycleReport.getStateCode()));
        yanganDataReport.setYanganVoltage((float)Byte.toUnsignedInt(yanganCycleReport.getVoltage())/10);
        yanganDataReport.setYanganSignal(Integer.parseInt
                (ConvertUtil.byteToString(yanganCycleReport.getSignal())));
        yanganDataReport.setReportTime(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        return yanganDataReport;
    }

    /**
     * 解释状态码
     * @param c
     * @return
     */
    private static int explainCID(byte[] c){
        if(c==null||c.length!=2){
            return 0;
        }
        int shi=Integer.parseInt(ConvertUtil.byteToString(c[0]))*100;
        int ge=Integer.parseInt(ConvertUtil.byteToString(c[1]));
        return shi+ge;
    }
}
